package com.huanletao.fork_join;

import com.huanletao.json.JacksonUtils;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.net.URIBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class demo {
    static CloseableHttpClient httpClient = HttpClients.createDefault();

    public static void main(String[] args) throws URISyntaxException, IOException, ParseException {
        URI uri = new URIBuilder("http://localhost:8086/seeyon/rest/thirdpartMessage/receive/singleMessage")
                .addParameter("token",login()).build();
        ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST,uri);
        request.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON));
        request.setEntity(new StringEntity(getMsg()));

        CloseableHttpResponse execute = httpClient.execute(request);

        String token = EntityUtils.toString(execute.getEntity());
        System.out.println("token = " + token);
    }

    private static String login() throws URISyntaxException, IOException, ParseException {
        URI uri = new URIBuilder("http://localhost:8086/seeyon/rest/token").build();
        Map<String, String> body = new HashMap<>();
        body.put("userName", "restadmin");
        body.put("password", "2687683d-a3a6-488f-8cdc-e323afdb1df2");

        ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST,uri);
        request.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON));
        request.setEntity(new StringEntity(JacksonUtils.toString(body)));
        CloseableHttpResponse execute = httpClient.execute(request);

        String token = EntityUtils.toString(execute.getEntity());
        Map<String, Object> javaMap = JacksonUtils.toJavaMap(token);
        String id = (String) javaMap.get("id");
        System.out.println("id = " + id);
        return id;
    }


    public static String getMsg(){
        Message message = new Message();
        message.setThirdpartyMessageId("12323123123121");
        message.setThirdpartyRegisterCode("A8");
        message.setMessageContent("测试集成发送消息");
        message.setThirdpartySenderId("100012");
        message.setThirdpartyReceiverId("1009231");
        message.setCreation_date("2023-03-16 14:41:00");
        message.setMessageURL("http://localhost:9088/message/data");
        message.setMessageH5URL("http://localhost:9088/message/data");
        return JacksonUtils.toString(message);
    }
}
